package com.corechina.apex_osswords.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.common.ShiroUtils;
import com.corechina.apex_osswords.domain.dto.CoreReminderClassificationDto;
import com.corechina.apex_osswords.domain.entity.CoreReminderClassification;
import com.corechina.apex_osswords.domain.dao.CoreReminderClassificationMapper;
import com.corechina.apex_osswords.domain.vo.CoreReminderClassificationVo;
import com.corechina.apex_osswords.service.ICoreIReminderClassificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;


/**
 * <p>
 * 提醒分类配置表 服务实现类
 * </p>
 *
 * @author fyh
 * @since 2024-02-19
 */
@Service
public class ICoreReminderClassificationServiceImpl extends ServiceImpl<CoreReminderClassificationMapper, CoreReminderClassification> implements ICoreIReminderClassificationService {

    @Autowired
    private CoreReminderClassificationMapper coreReminderClassificationMapper;

//    @Override
//    public Response getCorporateReminder(Integer enterprise_id) {
//        CoreReminderClassificationVo coreReminderClassificationVo = coreReminderClassificationMapper.getCorporateReminder(enterprise_id);
//        return Response.ok(coreReminderClassificationVo);
//    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Response add(CoreReminderClassificationDto coreReminderClassificationDto) {
        CoreReminderClassification coreReminderClassification = new CoreReminderClassification();
        BeanUtils.copyProperties(coreReminderClassificationDto, coreReminderClassification);
        coreReminderClassification.setCreator(ShiroUtils.getUserId());
        coreReminderClassification.setEnterpriseId(ShiroUtils.getEnterpriseId());
        coreReminderClassification.setCreateTime(LocalDateTime.now());
        //    coreProjectDto.setServiceId(coreProject.getServiceId());
        if(nameChecking(coreReminderClassification.getName())==0){
            coreReminderClassificationMapper.insert(coreReminderClassification);
            return Response.ok();
        } else {
            return Response.error("项目/设备提醒已存在");
        }
    }

    public Integer nameChecking(String name){
        CoreReminderClassificationVo coreReminderClassificationVo = coreReminderClassificationMapper.nameChecking(name);
        return coreReminderClassificationVo.getResult();
    }
    public Integer nameSearch(String name){
        CoreReminderClassificationVo coreReminderClassificationVo = coreReminderClassificationMapper.nameSearch(name);
        return coreReminderClassificationVo.getId();
    }

    @Override
    public Response getList(Integer classifying, String name, Integer creator, Integer enterpriseId, Integer pageIndex, Integer pageSize) {
        IPage<CoreReminderClassificationVo> reminderClassificationVoIPage = coreReminderClassificationMapper.getList(new Page(pageIndex, pageSize), classifying, name, creator, enterpriseId);
        return Response.ok(reminderClassificationVoIPage.getRecords(), reminderClassificationVoIPage.getTotal());
    }


    public Response del(String name){
        return null;
    }

}
